package com.qying.metrics;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;

import java.util.Queue;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class CounterTest {

    public static Queue<String> queue = new LinkedBlockingQueue<String>();

    public static Counter pendingJobs;

    public static Random random = new Random();

    public static void addJob(String job){
        pendingJobs.inc();
        queue.offer(job);
    }

    public static String takeJob(){
        pendingJobs.dec();
        return queue.poll();
    }

    public static void main(String[] args) throws InterruptedException {
        MetricRegistry metricRegistry = new MetricRegistry();
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
        reporter.start(1, TimeUnit.SECONDS);
        pendingJobs = metricRegistry.counter(MetricRegistry.name(Queue.class, "pending_jobs", "size"));

        int num = 1;
        while (true) {
            Thread.sleep(200);
            if (random.nextDouble() > 0.7) {
                String job = takeJob();
                System.out.println("take job:"+job);
            }else {
                String job = "job-" + num;
                addJob(job);
                System.out.println("add job :" + job);
            }
            num ++;
        }


    }

}
